package com.xuetang9.controller.sn.login;

import com.xuetang9.Query.AccountLoginQuery;
import com.xuetang9.Query.PhoneAccountQuery;
import com.xuetang9.controller.sn.login.domain.Message;
import com.xuetang9.dto.sn.MessageDTO;
import com.xuetang9.dto.sn.StateCode;
import com.xuetang9.dto.sn.VerificationCodeMessageDTO;
import com.xuetang9.service.sn.LoginService;
import com.xuetang9.utils.LoggerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @类: LoginController
 * @描述: 登录处理
 * @date: 2020/5/11
 * @author: Admin
 * @ver 1.0.0
 * @since JDK 1.8
 */
@Controller
@ResponseBody
@RequestMapping("/login")
@CrossOrigin
public class LoginController {

    /**
     * 登录服务类
     */
    private LoginService loginService;

    @Autowired
    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }


    @PostMapping("/account")
    @ResponseBody
    public MessageDTO login(@RequestBody
                            @Validated
                            AccountLoginQuery accountLoginQuery, BindingResult bindingResult) {
        MessageDTO messageDTO = new MessageDTO();
        //处理错误情况
        if (bindingResult.hasErrors()) {
            messageDTO.setFlag(false);
            messageDTO.setCode(StateCode.ILLEGAL);
            messageDTO.setMessage(bindingResult.getFieldError().getDefaultMessage());
            return messageDTO;
        }
        LoggerUtils.info("输入的信息为：{}",accountLoginQuery.toString());
        //执行登录
        messageDTO = loginService.login(accountLoginQuery);
        return messageDTO;
    }

    /**
     * 用于接收用户请求验证码
     * @param phoneAccountQuery 接收用户手机号
     * @param bindingResult
     * @return
     */
    @PostMapping("/code")
    public VerificationCodeMessageDTO getCode(@RequestBody @Validated(PhoneAccountQuery.GetCode.class) PhoneAccountQuery phoneAccountQuery,
                                              BindingResult bindingResult) {
        VerificationCodeMessageDTO verificationCodeMessageDTO = new VerificationCodeMessageDTO();
        LoggerUtils.debug("传入手机号：【{}】",phoneAccountQuery.toString());
        if (bindingResult.hasErrors()) {
            this.setErrorMessage(verificationCodeMessageDTO, bindingResult);
            return verificationCodeMessageDTO;
        }
        //发送验证码
        verificationCodeMessageDTO = loginService.getCode(phoneAccountQuery.getPhone());
        return verificationCodeMessageDTO;
    }

    /**
     * 组装错误信息
     * @param verificationCodeMessageDTO 返回错误的信息数据对象
     * @param bindingResult 记录验证信息的对象
     */
    private void setErrorMessage(VerificationCodeMessageDTO verificationCodeMessageDTO, BindingResult bindingResult) {
        //设置失败信息
        verificationCodeMessageDTO.setFlag(false);
        verificationCodeMessageDTO.setMessage(bindingResult.getFieldError().getDefaultMessage());
    }

    /**
     * 组装错误信息
     * @param verificationCodeMessageDTO 返回错误的信息数据对象
     * @param bindingResult 记录验证信息的对象
     */
    private void setErrorMessage(MessageDTO verificationCodeMessageDTO, BindingResult bindingResult) {
        //设置失败信息
        verificationCodeMessageDTO.setFlag(false);
        verificationCodeMessageDTO.setMessage(bindingResult.getFieldError().getDefaultMessage());
    }

    /**
     * 处理用户电话短信登录请求
     * @param bindingResult 后端验证信息记录
     * @return 反馈信息
     */
    @PostMapping("/phone")
    public MessageDTO login(@RequestBody @Validated({PhoneAccountQuery.Login.class}) PhoneAccountQuery phoneAccountQuery,
                                            BindingResult bindingResult) {
        MessageDTO MessageDTO = new MessageDTO();
        if (bindingResult.hasErrors()) {
            setErrorMessage(MessageDTO, bindingResult);
            return MessageDTO;
        }
        //验证登录
        try {
            MessageDTO = loginService.phoneLogin(phoneAccountQuery);
        } catch (Exception e) {
            MessageDTO.setFlag(false);
            MessageDTO.setMessage("服务器繁忙！");
            //讲异常进行记录，以文本形式记录，日志的内容要记录核心信息方便追踪
            LoggerUtils.getLogger().error("账号[{}]登录验证异常！[{}]",phoneAccountQuery.getPhone(),e.getMessage());

        }
        return MessageDTO;
    }
}
